package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.course.domain.CourseFloor;
import com.ruoyi.course.dto.bo.CommonRespBO;
import com.ruoyi.course.dto.bo.CourseClassBO;
import com.ruoyi.course.mapper.CourseFloorMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CourseClassMapper;
import com.ruoyi.course.domain.CourseClass;
import com.ruoyi.course.service.ICourseClassService;

/**
 * 教室Service业务层处理
 *
 * @author mac
 * @date 2023-03-11
 */
@Service public class CourseClassServiceImpl implements ICourseClassService {
    @Autowired private CourseClassMapper courseClassMapper;
    @Autowired private CourseFloorMapper courseFloorMapper;

    /**
     * 查询教室
     *
     * @param id 教室主键
     * @return 教室
     */
    @Override public CourseClass selectCourseClassById(Long id) {
        return courseClassMapper.selectCourseClassById(id);
    }

    /**
     * 查询教室列表
     *
     * @param courseClass 教室
     * @return 教室
     */
    @Override public List<CourseClassBO> selectCourseClassList(CourseClass courseClass) {
        List<CourseClass> classList = courseClassMapper.selectCourseClassList(courseClass);
        List<CourseClassBO> bos = new ArrayList<>();
        classList.forEach(t -> {
            CourseClassBO bo = new CourseClassBO();
            CourseFloor floor = courseFloorMapper.selectCourseFloorById(t.getFloorId());
            bo.setFloorName(floor.getFloorName());
            BeanUtils.copyProperties(t, bo);
            bos.add(bo);
        });
        return bos;
    }

    /**
     * 新增教室
     *
     * @param
     * @return 结果
     */
    @Override public int insertCourseClass(CourseClassBO classBO) {
        CourseFloor floor = courseFloorMapper.selectCourseFloorByName(classBO.getFloorName());
        if (floor == null) {
            throw new RuntimeException("楼栋不存在！");
        }

        CourseClass courseClass = new CourseClass();
        courseClass.setClassName(classBO.getClassName());
        courseClass.setFloorId(floor.getId());

        CourseClass aClass = courseClassMapper.selectCourseClassByCourseClass(courseClass);
        if (aClass != null) {
            throw new RuntimeException("该教室信息与楼栋已绑定！");
        }

        return courseClassMapper.insertCourseClass(courseClass);
    }

    /**
     * 修改教室
     *
     * @param courseClass 教室
     * @return 结果
     */
    @Override public int updateCourseClass(CourseClass courseClass) {
        return courseClassMapper.updateCourseClass(courseClass);
    }

    /**
     * 批量删除教室
     *
     * @param ids 需要删除的教室主键
     * @return 结果
     */
    @Override public int deleteCourseClassByIds(Long[] ids) {
        return courseClassMapper.deleteCourseClassByIds(ids);
    }

    /**
     * 删除教室信息
     *
     * @param id 教室主键
     * @return 结果
     */
    @Override public int deleteCourseClassById(Long id) {
        return courseClassMapper.deleteCourseClassById(id);
    }

    @Override public List<CommonRespBO> listFloorName() {
        List<CourseFloor> floors = courseFloorMapper.selectCourseFloorList(null);

        List<CommonRespBO> bos = new ArrayList<>();
        floors.forEach(t -> {
            CommonRespBO bo = new CommonRespBO();
            bo.setLabel(t.getFloorName());
            bo.setValue(t.getId());
            bos.add(bo);
        });
        return bos;
    }
}
